﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace TheOliver.Phone.BMI
{
    public partial class History : PhoneApplicationPage
    {
        public History()
        {
            InitializeComponent();

            _list.SelectionChanged += (s, e) =>
            {
                _removeButton.IsEnabled = _list.SelectedItem != null;
            };
        }

        ApplicationBarIconButton _removeButton;

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            // Load Data
            BMIDataContext db = new BMIDataContext();
            this.DataContext = db.Items;

            // Set Application Bar Icon
            _removeButton = (ApplicationBarIconButton)ApplicationBar.Buttons[0];
            _removeButton.Text = Strings.Strings.Delete;
            _removeButton.Click += (s, e1) =>
            {
                BMIItem item = (BMIItem)_list.SelectedItem;
                db.DeleteBmi(item);

                this.DataContext = null;
                this.DataContext = db.Items;
            };
        }
    }
}